/*------------------------------------------------------------------------*/
/* Sample code of OS dependent controls for FatFs                         */
/* (C)ChaN, 2014                                                          */
/*------------------------------------------------------------------------*/


#include "../include/ff.h"


#if _FS_REENTRANT
/*------------------------------------------------------------------------*/
/* Create a Synchronization Object
   This function is called in f_mount() function to create a new
   synchronization object, such as semaphore and mutex. When a 0 is returned,
   the f_mount() function fails with FR_INT_ERR.
*/

int ff_cre_syncobj (    /* !=0:Function succeeded, ==0:Could not create due to any error */
    BYTE vol,           /* Corresponding logical drive being processed */
    _SYNC_t *sobj       /* Pointer to return the created sync object */
)
{
    UINT uwRet;

    uwRet = LOS_MuxCreate(sobj);
    if (uwRet == LOS_OK )
    {
        return TRUE;
    }
    else
        return FALSE;
}



/*------------------------------------------------------------------------*/
/* Delete a Synchronization Object                                        */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount() function to delete a synchronization
/  object that created with ff_cre_syncobj function. When a 0 is returned,
/  the f_mount() function fails with FR_INT_ERR.
*/

int ff_del_syncobj (    /* !=0:Function succeeded, ==0:Could not delete due to any error */
    _SYNC_t sobj            /* Sync object tied to the logical drive to be deleted */
)
{
    UINT uwRet;
    uwRet = LOS_MuxDelete(sobj);
    if(uwRet == LOS_OK)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}



/*------------------------------------------------------------------------*/
/* Request Grant to Access the Volume                                     */
/*------------------------------------------------------------------------*/
/* This function is called on entering file functions to lock the volume.
/  When a 0 is returned, the file function fails with FR_TIMEOUT.
*/

int ff_req_grant (  /* 1:Got a grant to access the volume, 0:Could not get a grant */
    _SYNC_t sobj       /* Sync object to wait */
)
{
    if (LOS_MuxPend(sobj, _FS_TIMEOUT) == LOS_OK)
    {
        return TRUE;
    }

    return FALSE;
}



/*------------------------------------------------------------------------*/
/* Release Grant to Access the Volume                                     */
/*------------------------------------------------------------------------*/
/* This function is called on leaving file functions to unlock the volume.
*/

void ff_rel_grant (
    _SYNC_t sobj    /* Sync object to be signaled */
)
{
    (void)LOS_MuxPost(sobj);
}

#endif

void ff_memset (void* dst, int val, UINT cnt) {
    BYTE *d = (BYTE*)dst;

    do {
        *d++ = (BYTE)val;
    } while (--cnt);
}


int ff_strnlen(const void *str, size_t maxlen)
{
    const BYTE *p = (const BYTE *)NULL;
    const BYTE *c = (const BYTE*)str;
    for (p = c; maxlen-- != 0 && *p != '\0'; ++p) ;

    return p - c;
}
/*------------------------------------------------------------------------*/
/* Allocate a memory block                                                */
/*------------------------------------------------------------------------*/
/* If a NULL is returned, the file function fails with FR_NOT_ENOUGH_CORE.
*/

void* ff_memalloc ( /* Returns pointer to the allocated memory block */
    UINT msize          /* Number of bytes to allocate */
)
{
    void* ptr = NULL;

    if(msize == 0)
        return NULL;

    ptr = LOS_MemAlloc((void *)OS_SYS_MEM_ADDR,  msize);  /* Allocate a new memory block*/

    if(ptr != NULL)
    {
        ff_memset((void *)ptr, (int)0, msize);
    }

    return ptr;
}


/*------------------------------------------------------------------------*/
/* Free a memory block                                                    */
/*------------------------------------------------------------------------*/
void ff_memfree (
    void* mblock    /* Pointer to the memory block to free */
)
{
        if(mblock == NULL)
            return;

    LOS_MemFree((void *)OS_SYS_MEM_ADDR, mblock);/*lint !e534*/
}

